<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryPersonList" :inline="true" label-width="68px">
        <el-form-item label="人员名称">
          <el-input v-model="queryPersonList.name" placeholder="请输入查询人姓名" clearable style="width: 150px" />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="queryPersonList.timeList" type="datetimerange" range-separator="~"
            start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-hasPermi="['report:lifeCycle:query']"
            @click="personQuery">查询</el-button>
          <el-button type="danger" icon="el-icon-refresh" @click="clearAll">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="personList" border height="605px" :default-sort = "{prop: 'date', order: 'descending'}">
        <el-table-column show-overflow-tooltip prop="CheckDate" label="日期" sortable align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="BillsID" label="单据号" align="center"></el-table-column>
        <!-- <el-table-column  show-overflow-tooltip prop="CustomerName" label="往来单位" sortable align="center"></el-table-column> -->
        <el-table-column show-overflow-tooltip prop="spbh" label="商品编号" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="DrugName" label="商品名称" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="DrugGG" label="商品规格" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="DrugBatchNo" label="批号" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="BillsCD" label="生产厂家" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="BillsType" label="单据类型" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="UserName" label="操作人" align="center"></el-table-column>
        <!-- <el-table-column show-overflow-tooltip prop="SecondUser" label="第二复核人" align="center"></el-table-column> -->
        <el-table-column show-overflow-tooltip prop="DrugNum" label="实际数量" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="DrugCheckNum" label="确认数量" align="center"></el-table-column>
      </el-table>
      <el-pagination background layout=" total,prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-size="pageSize"
        :page-sizes="[20, 50, 100]" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  import {
    findAuditUser,
  } from "@/api/report/index.js";
  export default {
    data() {
      return {
        queryPersonList: {
          name: '',
          timeList: []
        },
        personList: [],
        pageNum: 1,
        pageSize: 20,
        total: 0,
      }
    },
    created() {
      this.getPerson();
    },
    methods: {
      handleSizeChange(newsize) {
        this.pageNum = 1;
        this.pageSize = newsize;
        this.getPerson();
      },
      handleCurrentChange(newpage) {
        this.pageNum = newpage;
        this.getPerson();
      },
      personQuery() {
        this.getPerson();
      },
      getPerson() {
        // 检查 timeList 是否为有效的数组并且包含至少两个元素
        const [beginDate, endDate] = Array.isArray(this.queryPersonList.timeList) && this.queryPersonList.timeList
          .length === 2 ?
          this.queryPersonList.timeList :
          [null, null]; // 如果不是有效的时间范围，给默认值 [null, null]
        // 构建请求参数，始终包括用户名称（如果有）
        let param = {
          userId: this.queryPersonList.name, // 如果没有输入用户名，默认为 'admin'
          pageNum: this.pageNum,
          pageSize: this.pageSize
        };
        // 如果日期有效，才传递 beginDate 和 endDate
        if (beginDate && endDate) {
          param.beginDate = beginDate;
          param.endDate = endDate;
        }
        // 调用查询接口
        findAuditUser(param).then(res => {
          this.personList = res.data.rows;
          this.total = res.data.total;
        }).catch(error => {
          console.error('查询失败：', error);
        });
      },
      clearAll() {
        // 清空查询条件
        this.queryPersonList.name = '';
        this.queryPersonList.timeList = [];
        this.pageNum = 1
        this.personQuery(); // 重置后重新查询
      }
    }
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding: 20px;
  }

  .el-card {
    margin-bottom: 20px;
  }

  .el-pagination {
    float: right;
    margin: 20px;
  }
</style>
